//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using System.Diagnostics;

namespace OpenLS.Chart
{
    public class StockChart : ChartBase, IDataLabelsOwner
    {
        private DataLabels dlbls;
        private ChartLines dropLines;
        private Axis firstAxis;
        private LineSeries firstLineSeries;
        private LineSeries fourthLineSeries;
        private ChartLines hiLowLines;
        private Axis secondAxis;

        private LineSeries secondLineSeries;

        private LineSeries thirdLineSeries;
        private UpDownBars upDownBars;

        public LineSeries FirstLineSeries
        {
            get { return firstLineSeries; }
            set { firstLineSeries = value; notifyPropertyChanged("FirstLineSeries");}
        }

        public LineSeries SecondLineSeries
        {
            get { return secondLineSeries; }
            set { secondLineSeries = value; notifyPropertyChanged("SecondLineSeries");}
        }

        public LineSeries ThirdLineSeries
        {
            get { return thirdLineSeries; }
            set { thirdLineSeries = value; notifyPropertyChanged("ThirdLineSeries");}
        }

        public LineSeries FourthLineSeries
        {
            get { return fourthLineSeries; }
            set { fourthLineSeries = value; notifyPropertyChanged("FourthLineSeries");}
        }

        public Axis FirstAxis
        {
            get { return firstAxis; }
            set { firstAxis = value; }
        }

        public Axis SecondAxis
        {
            get { return secondAxis; }
            set { secondAxis = value; }
        }


        public ChartLines DropLines
        {
            get { return dropLines; }
            set { dropLines = value; notifyPropertyChanged("DropLines");}
        }

        public ChartLines HighLowLines
        {
            get { return hiLowLines; }
            set { hiLowLines = value; notifyPropertyChanged("HighLowLines");}
        }


        public UpDownBars UpDownBars
        {
            get { return upDownBars; }
            set { upDownBars = value; notifyPropertyChanged("UpDownBars");}
        }


        public DataLabels DataLabels
        {
            get { return dlbls; }
            set { dlbls = value; notifyPropertyChanged("DataLabels");}
        }

        protected override void VerifyAxes()
        {
            Debug.WriteLine("todo //\\" + GetType());
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("stockChart"))
            {
                FirstLineSeries = new LineSeries();
                FirstLineSeries.ReadXmlCore(c);
                SecondLineSeries = new LineSeries();
                SecondLineSeries.ReadXmlCore(c);
                ThirdLineSeries = new LineSeries();
                ThirdLineSeries.ReadXmlCore(c);
                if (c.ReaderLocalName == "ser")
                {
                    FourthLineSeries = new LineSeries();
                    FourthLineSeries.ReadXmlCore(c);
                }
                else
                    FourthLineSeries = null;
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                HighLowLines = c.ReadOptionalElementAs<ChartLines>("hiLowLines");
                UpDownBars = c.ReadOptionalElement<UpDownBars>("upDownBars");
                AxisResolver r = ReadContextData.GetAxisResolver(c);
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { FirstAxis = a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { SecondAxis = a; });
                c.WarnIf("ext");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("stockChart"))
            {
                c.WriteElement(FirstLineSeries);
                c.WriteElement(SecondLineSeries);
                c.WriteElement(ThirdLineSeries);
                c.WriteOptionalElement(FourthLineSeries);
                c.WriteOptionalElement(DataLabels);
                c.WriteOptionalElementAs(DropLines, "dropLines");
                c.WriteOptionalElementAs(HighLowLines, "hiLowLines");
                c.WriteOptionalElement(UpDownBars);
                c.WriteElementUIntegerAttribute("axId", FirstAxis.Id);
                c.WriteElementUIntegerAttribute("axId", SecondAxis.Id);
                c.WarnIf("ext");
            }
        }

        internal override IEnumerable<Axis> GetAxes()
        {
            yield return FirstAxis;
            yield return SecondAxis;
        }
    }
}